package com.tul.vapp.servicecenter.util;

import java.util.Locale;
import java.util.Map;

import com.tul.vapp.servicecenter.common.ErrorCodeConstant;
import com.tul.vapp.servicecenter.common.SysConfigConstant;
import org.apache.commons.lang3.StringUtils;

import com.tul.cusa.common.exception.BizException;
import com.tul.cusa.common.exception.SysException;
import com.tul.cusa.common.log.LogTracerFactory;
import com.tul.cusa.common.log.intf.ILogTracer;
import com.tul.cusa.common.util.StringUtil;


/**
 * Created by jyliu on 2016/10/22.
 */
public class MessageUtil {
	/**
	 * 日志
	 */
	private static final ILogTracer TRACER = LogTracerFactory.getTracer(MessageUtil.class);

	/**
	 * 消息资源文件默认路径
	 */
	private static final String DEFAULT_MESSAGE_RESOURCES =
            "localization/message/error,localization/message/info";

	/**
	 * 资源路径
	 */
	private static String[] resList = SysConfigUtil
			.getProperty(SysConfigConstant.MESSAGE_RESOURCES, DEFAULT_MESSAGE_RESOURCES).split(",");

	/**
	 * 读取国际化消息
	 * 
	 * @param messageKey 消息Key
	 * @return 资源详细信息
	 */
	public static String getMessage(String messageKey) {
		return getMessage(messageKey, null, null);
	}

	/**
	 * 读取国际化消息
	 * 
	 * @param messageKey 消息Key
	 * @param locale 语言包
	 * @return 资源详细信息
	 */
	public static String getMessage(String messageKey, Locale locale) {
		return getMessage(messageKey, null, locale);
	}

	/**
	 * 读取国际化消息
	 * 
	 * @param messageKey 消息Key
	 * @param mapParas 解析Key的上下文
	 * @return 资源详细信息
	 */
	public static String getMessage(String messageKey, Map mapParas) {
		return getMessage(messageKey, mapParas, null);
	}

	/**
	 * 读取国际化消息
	 * 
	 * @param messageKey 消息Key
	 * @param mapParas 解析Key的上下文
	 * @param locale 语言包
	 * @return 资源详细信息
	 */
	public static String getMessage(String messageKey, Map mapParas, Locale locale) {
		if (StringUtils.isEmpty(messageKey)) {
			return "";
		}
		if (null == locale) {
			locale = I18nUtil.getLocale();
		}
		String retStr = messageKey;

		boolean found = false;
		for (String resPath : resList) {
			try {
				String result = I18nUtil.getResource(resPath, messageKey, locale);
				if (StringUtils.isNotEmpty(result)) {
					retStr = result;
					found = true;
					break;
				}
			} catch (Exception e) {
				// TRACER.debug(e.getMessage());
			}
		}
		if (!found) {
			TRACER.debug("Message key " + messageKey + " is not found in resource files.");
		}

		return StringUtil.replaceParams(retStr, mapParas);
	}

	/**
	 * 处理系统异常消息
	 * 
	 * @param ae 异常
	 * @return 资源详细信息
	 */
	public static String getExceptionMsg(Exception ae) {
		String errorCode = null;
		String errorMsg = null;
		if (ae instanceof BizException) {
			errorMsg = ((BizException) ae).getErrorMsg();
			if (StringUtil.isEmpty(errorMsg)) {
				errorCode = StringUtil.replaceNull(((BizException) ae).getErrorCode(),
						ErrorCodeConstant.BIZ_UNKNOWN);
				errorMsg = MessageUtil.getMessage(errorCode, ((BizException) ae).getContextMap());
			}
		} else if (ae instanceof SysException) {
			errorMsg = ((SysException) ae).getErrorMsg();
			if (StringUtil.isEmpty(errorMsg)) {
				errorCode = StringUtil.replaceNull(((SysException) ae).getErrorCode(),
						ErrorCodeConstant.SYS_UNKNOWN);
				errorMsg = MessageUtil.getMessage(errorCode, ((SysException) ae).getContextMap());
			}
		} else {
			errorCode = ErrorCodeConstant.SYS_UNKNOWN;
			errorMsg = MessageUtil.getMessage(errorCode);
		}

		return errorMsg;
	}
}
